package com.sxkf.template.controller;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.sxkf.core.api.vo.R;
import com.sxkf.core.third.os.OsService;
import com.sxkf.core.third.os.vo.response.UploadFileVO;
import com.sxkf.template.utils.FileToMultipartFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.exception.SxkfException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@RestController
@Api(tags = "生成二维码")
@RequestMapping("/qrCode")
public class ShareController {

    @Resource(name = "ossServiceImpl")
    private OsService osService;


    @GetMapping("/gen")
    @ApiOperation("字符串生成二维码，返回二维码链接")
    public R<String> gen(@RequestParam String text) {

        // 设置二维码的配置，例如尺寸等
        QrConfig config = new QrConfig(300, 300);
        // 生成位置
        File file = new File(System.getProperty("user.dir") + "/qrcode.png");
        QrCodeUtil.generate(text, config, file);

        FileToMultipartFileUtil multipartFileUtil = new FileToMultipartFileUtil(file);
        try {
            UploadFileVO uploadFileVO = osService.uploadFile(multipartFileUtil);
            file.delete();
            return R.success(uploadFileVO.getUrl());
        } catch (IOException e) {
            e.printStackTrace();
        }
        throw new SxkfException("生成失败");

    }

}
